/**
 * Project Name:basicServiceAPI
 * File Name:UUIDUtil.java
 * Package Name:com.gdsalt.api.saltbasicservice.util
 * Date:2017年10月17日上午10:25:18
 *
*/

package com.gidgabs.utils;
/**
 * ClassName:UUIDUtil <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2017年10月17日 上午10:25:18 <br/>
 * @author   channing
 * @version
 * @see 	 
 */
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.UUID;

/**
 * 生成有序的32位UUID
 * 
 * @author channing
 *
 */
public class UUIDUtil {

	private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
			.toCharArray();
	private static final int[] INDEXES = new int[128];

	static {
		for (int i = 0; i < INDEXES.length; i++) {
			INDEXES[i] = -1;
		}
		for (int i = 0; i < ALPHABET.length; i++) {
			INDEXES[ALPHABET[i]] = i;
		}
	}

	/**
	 * Encodes the given bytes in base58. No checksum is appended.
	 */
	private  static String encode(byte[] input) {
		if (input.length == 0) {
			return "";
		}
		input = copyOfRange(input, 0, input.length);
		// Count leading zeroes.
		int zeroCount = 0;
		while (zeroCount < input.length && input[zeroCount] == 0) {
			++zeroCount;
		}
		// The actual encoding.
		byte[] temp = new byte[input.length * 2];
		int j = temp.length;

		int startAt = zeroCount;
		while (startAt < input.length) {
			byte mod = divmod58(input, startAt);
			if (input[startAt] == 0) {
				++startAt;
			}
			temp[--j] = (byte) ALPHABET[mod];
		}

		// Strip extra '1' if there are some after decoding.
		while (j < temp.length && temp[j] == ALPHABET[0]) {
			++j;
		}
		// Add as many leading '1' as there were leading zeros.
		while (--zeroCount >= 0) {
			temp[--j] = (byte) ALPHABET[0];
		}

		byte[] output = copyOfRange(temp, j, temp.length);
		try {
			return new String(output, "US-ASCII");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e); // Cannot happen.
		}
	}

	//
	// number -> number / 58, returns number % 58
	//
	private  static byte divmod58(byte[] number, int startAt) {
		int remainder = 0;
		for (int i = startAt; i < number.length; i++) {
			int digit256 = (int) number[i] & 0xFF;
			int temp = remainder * 256 + digit256;

			number[i] = (byte) (temp / 58);

			remainder = temp % 58;
		}

		return (byte) remainder;
	}

	private  static byte[] copyOfRange(byte[] source, int from, int to) {
		byte[] range = new byte[to - from];
		System.arraycopy(source, from, range, 0, range.length);

		return range;
	}

	private static String changeUUID(UUID uuid) {
		String str = uuid.toString();
		String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
		return temp;
	}
	
	private  static String getUUID(Date date, UUID uuid, String code) {
		if(code == null || code.length() == 0){
			return changeUUID(uuid) ;
		}
		if(code.length() > 4){
			code = code.substring(0,4) ;
		}
		ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
		bb.putLong(uuid.getMostSignificantBits());
		bb.putLong(uuid.getLeastSignificantBits());
		long time = System.currentTimeMillis();
		String temp = code+ Long.toString(time, 18).substring(4)+ encode(bb.array());
		return temp.toLowerCase();
	}

	public static String getUUID(String code) {
		return getUUID(new Date(), UUID.randomUUID(), code);
	}
	
}

